
export function exportFile ({ url = '', param = {}, fileName = '导出文件' }) {
  const { $axios, $message, $get } = this
  if (!url) return false
  this.$toggleSending(true)
  return new Promise((resolve, reject) => {
    $axios.post(url, param, {
      responseType: 'arraybuffer'
    }).then(res => {
      this.$toggleSending(false)
      if (!res.data.code) {
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })

        const reader = new FileReader()
        reader.onload = function (event) {
          let res = {}
          try {
            res = JSON.parse(reader.result)
          } catch (e) {
            res = {}
          }
          if ($get(res, 'code', '000000') !== '000000') {
            $message.destroy()
            $message.error(`${$get(res, 'mesg', '')}请更换导出条件，或稍后再试`)
            resolve(false)
            return false
          }

          const linkNode = document.createElement('a')
          linkNode.download = `${fileName}.xlsx` // a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none'
          linkNode.href = window.URL.createObjectURL(blob) // 生成一个Blob URL
          document.body.appendChild(linkNode)
          linkNode.click() // 模拟在按钮上的一次鼠标单击
          window.URL.revokeObjectURL(linkNode.href) // 释放URL 对象
          document.body.removeChild(linkNode)
          resolve(true)
        }
        reader.readAsText(blob)
      } else resolve(false)
    }).catch(_ => {
      this.$toggleSending(false)
      $message.destroy()
      $message.error('请更换导出条件或稍后再试')
      resolve(false)
    })
  })
}
